

import framework.GameContext;
import framework.Screen;
import gamebackend.GameSession;
import gamebackend.GameSession.FlierListener;
import gamebackend.Ship;
import gui.TouchToFlyOverlay;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javaframework.JavaGameContext;
import javaframework.JavaGameContext.JavaScreen;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class JavaPlayScreen extends JavaScreen implements FlierListener, ActionListener {

	
	
	private GameSession my_session;
	private Screen my_controls;
	private final JPanel my_pauseMenu;
	private final JButton my_continueButton;
	private final JButton my_restartButton;
	private final JButton my_changeShipButton;
	private final JavaGameContext my_context;
	
	private final JPanel my_deathScreen;
	private final JButton my_newGameButton;
	private final JButton my_newShipButton;
	private final JLabel my_finalScoreLabel;
	private final JLabel my_finalTimeLabel;
	private final JTextArea my_seedText;
	private final Class<? extends Ship> my_shipClass;

	/**
	 * @param javaGameContext
	 * @param the_ship LASER_SHIP or CLONE_SHIP
	 */
	public JavaPlayScreen(JavaGameContext javaGameContext, Class<? extends Ship> the_class) {
		javaGameContext.super(javaGameContext);
		JPanel p = new JPanel();
		my_shipClass = the_class;
		
		p.setLayout(new GridLayout(0, 1));
		my_continueButton = new JButton("Continue");
		my_continueButton.addActionListener(this);
		p.add(my_continueButton);
		my_restartButton = new JButton("Restart");
		my_restartButton.addActionListener(this);
		p.add(my_restartButton);
		my_changeShipButton = new JButton("Change ship");
		my_changeShipButton.addActionListener(this);
		p.add(my_changeShipButton);
		my_pauseMenu = p;
		addSwing(my_pauseMenu);
		p.setVisible(false);
		
		p = new JPanel();
		p.setLayout(new GridLayout(0, 1));
		my_deathScreen = p;
		my_newGameButton = new JButton("Restart");
		my_newGameButton.addActionListener(this);
		my_newShipButton = new JButton("Change ship");
		my_newShipButton.addActionListener(this);
		my_finalScoreLabel = new JLabel("Final Score:");
		my_finalTimeLabel = new JLabel("Final time:");
		my_seedText = new JTextArea();
		my_seedText.setEditable(false);
		p.add(my_seedText);
		p.add(my_finalTimeLabel);
		p.add(my_finalScoreLabel);
		p.add(my_newGameButton);
		p.add(my_newShipButton);
		addSwing(p);
		p.setVisible(false);
		
		my_context = javaGameContext;
		restart();
		
	}
	private void restart() {
		if (my_session != null) {
			my_session.dispose();
		}
		my_session = new GameSession(my_context, this, my_shipClass);
		my_controls = new TouchToFlyOverlay(my_context, my_session);
	}

	@Override
	public void step(int the_deltaTime) {
		// TODO Auto-generated method stub
		my_controls.step(the_deltaTime);
	}

	@Override
	public void render() {
		my_controls.render();
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		my_controls.dispose();
	}

	@Override
	public void onGameEnded(int time, int score, long the_seed) {
		my_finalScoreLabel.setText("Score: " + score);
		final StringBuilder sb = new StringBuilder();
	    final int s = time / 1000 % 60;
	    final int m = time / 1000 / 60;
		my_finalTimeLabel.setText("Time: " + (m < 10 ? "0" : "") + m
		    	+ (s < 10 ? ":0" : ":") + s);
		my_deathScreen.setVisible(true);
		my_newGameButton.requestFocusInWindow();
		my_seedText.setText("Seed: " + the_seed);
		
		
	}

	@Override
	public void onPause() {
		my_pauseMenu.setVisible(true);
		my_continueButton.requestFocusInWindow();
	}
	@Override
	public void keyPressed(final int the_key) {
		my_controls.keyPressed(the_key);
	}
	@Override
	public void keyReleased(final int the_key) {
		my_controls.keyReleased(the_key);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		final Object source = e.getSource();
		if (source == my_continueButton) {
			my_pauseMenu.setVisible(false);
			my_session.unpause();
		} else if (source == my_restartButton) {
			restart();
			my_pauseMenu.setVisible(false);
			my_session.unpause();
		} else if (source == my_changeShipButton || source == my_newShipButton) {
//			my_pauseMenu.setVisible(false);
			
			my_session.dispose();
			my_game.setScreen(new JavaStartScreen(my_context));
		} else if (source == my_newGameButton) {
			restart();
			// setting visible false before restart didn't 
			// remove screen when crashing right at the start
			my_deathScreen.setVisible(false);
		}
		
	}
	

}
